﻿@{
    ViewBag.Title = "Form"; 
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script>
    layui.use(['jquery', 'table', 'form', 'laydate', 'commonTable', 'common', 'optimizeSelectOption'], function () {
        var form = layui.form,
            $ = layui.$,
            table = layui.table,
            laydate = layui.laydate,
            commonTable = layui.commonTable,
            common = layui.common;
        var keyValue = $.request("keyValue");
        var processId;
        var materialId;
        //权限字段
        common.authorizeFields('adminform');
        //执行一个laydate实例
        laydate.render({
            elem: '#F_PlanStartTime'
            , trigger: 'click'
            , type: 'datetime'
            , format: 'yyyy/MM/dd HH:mm:ss',
        });
        //执行一个laydate实例
        laydate.render({
            elem: '#F_PlanEndTime'
            , trigger: 'click'
            , type: 'datetime'
            , format: 'yyyy/MM/dd HH:mm:ss',
        });
        $(function () {
            if (!!keyValue) {
                common.ajax({
                    url: '/ProcessManage/WorkOrderRun/GetFormJson',
                    dataType: 'json',
                    data: { keyValue: keyValue },
                    async: false,
                    success: function (data) {
                        common.val('adminform', data);
                        processId = data.F_ProcessId;
                        materialId = data.F_MaterialId;
                        common.setReadOnly('adminform');
                        $('#F_MouldNum').parent().parent().addClass('layui-hide');
                        if (data.listData) {
                            var datas = JSON.parse(data.listData);
                            for (var i = 0; i < datas.length; i++) {
                                if (datas[i].F_EqpUse == 7) {
                                    $('#F_MouldNum').val(datas[i].F_MouldNum);
                                    $('#F_MouldNum').parent().parent().removeClass('layui-hide');
                                    break;
                                }
                            }
                            table.reload('currentTableId', {
                                data: datas
                            });
                        }
                        if (data.userTimeList) {
                            table.reload('currentTable', {
                                data: data.userTimeList
                            });
                        }
                    }
                });
            }
            form.render();
        });
        wcLoading.close();
        commonTable.rendertable({
            elem: '#currentTableId',
            id: 'currentTableId',
            data: [],
            search: false,
            limit: 9999,//每页数据 默认
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count'] //自定义分页布局
                , first: false //不显示首页
                , last: false //不显示尾页
            },
            height: 'full-280',
            cols: [[
                { type: "checkbox", width: 50 },
                {
                    field: 'F_EqpType', title: '设备类型', width: 120,
                    templet: function (d) {
                        return top.clients.dataItems["Mes_EqpType"][d.F_EqpType] == null ? "" : top.clients.dataItems["Mes_EqpType"][d.F_EqpType];
                    }
                },
                {
                    field: 'F_EqpUse', title: '设备用途', width: 120,
                    templet: function (d) {
                        return top.clients.dataItems["Mes_EqpUse"][d.F_EqpUse] == null ? "" : top.clients.dataItems["Mes_EqpUse"][d.F_EqpUse];
                    }
                },
                { field: 'F_EqpName', title: '设备名称', width: 120 },

                {
                    field: 'F_EqpState', title: '设备状态', width: 120,
                    templet: function (d) {
                        return top.clients.dataItems["Mes_EqpState"][d.F_EqpState] == null ? "" : top.clients.dataItems["Mes_EqpState"][d.F_EqpState];
                    }
                },
                {
                    field: 'F_IsMaster', title: '主设备', width: 100,
                    templet: function (d) {
                        if (d.F_IsMaster == true) {
                            return "<span class='layui-btn layui-btn-normal layui-btn-xs'>是</span>";
                        } else {
                            return "<span class='layui-btn layui-btn-warm layui-btn-xs'>否</span>";
                        }
                    }
                },
                {
                    field: 'F_IsOtherEqp', title: '加载', width: 80,
                    templet: function (d) {
                        if (d.F_IsOtherEqp == true) {
                            return "<span class='layui-btn layui-btn-normal layui-btn-xs'>是</span>";
                        } else {
                            return "<span class='layui-btn layui-btn-warm layui-btn-xs'>否</span>";
                        }
                    }
                },
                { field: 'F_MoldingNum', title: '模块数', width: 80 },
                { field: 'F_Description', title: '设备描述', minWidth: 120 },
            ]]
        });
        commonTable.rendertable({
            elem: '#currentTable',
            id: 'currentTable',
            toolbar: '#toolDemo',//工具栏
            data: [],
            search: false,
            limit: 9999,//每页数据 默认
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count'] //自定义分页布局
                , first: false //不显示首页
                , last: false //不显示尾页
            },
            height: 'full-280',
            cols: [[
                { type: "checkbox", width: 50 },
                { field: 'F_UserName', title: '值班人', width: 120 },
                {
                    field: 'F_StartTime', title: '开始时间', width: 150,
                    templet: function (d) {
                        if (d.F_StartTime) {
                            var time = new Date(d.F_StartTime);
                            return time.Format("yyyy-MM-dd hh:mm");
                        }
                        return '';
                    }
                },
                {
                    field: 'F_EndTime', title: '结束时间', width: 150,
                    templet: function (d) {
                        if (d.F_EndTime) {
                            var time = new Date(d.F_EndTime);
                            return time.Format("yyyy-MM-dd hh:mm");
                        }
                        return '';
                    }
                },
                {
                    field: 'F_TimeNum', title: '时长', width: 100
                },
                { field: 'F_Description', title: '备注', minWidth: 120, edit: 'text' }
            ]]
        });
        $(document).on("click", ".layui-table-body table.layui-table tbody tr", function () {
            var index = $(this).attr('data-index');
            var tableBox = $(this).parents('.layui-table-box');
            //存在固定列
            if (tableBox.find(".layui-table-fixed.layui-table-fixed-l").length > 0) {
                tableDiv = tableBox.find(".layui-table-fixed.layui-table-fixed-l");
            } else {
                tableDiv = tableBox.find(".layui-table-body.layui-table-main");
            }
            var checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.laytable-cell-checkbox div.layui-form-checkbox I");
            if (checkCell.length > 0) {
                checkCell.click();
            }
        });
        //对td的单击事件进行拦截停止，防止事件冒泡再次触发上述的单击事件（Table的单击行事件不会拦截，依然有效）
        $(document).on("click", "td div.laytable-cell-checkbox div.layui-form-checkbox", function (e) {
            e.stopPropagation();
        });
        //多选监听事件
        table.on('checkbox(currentTableFilter)', function (obj) {
            var data = table.checkStatus('currentTableId').data
            if (obj.type == "all") {
                if (obj.checked && table.cache.currentTableId.length != 0) {
                    $('[name="NF-delete"]').removeClass("layui-hide");
                }
                else {
                    $('[name="NF-delete"]').addClass("layui-hide");
                }
            }
            else {
                if (data.length > 0) {
                    $('[name="NF-delete"]').removeClass("layui-hide");
                }
                else {
                    $('[name="NF-delete"]').addClass("layui-hide");
                }
            }
        });
        //多选监听事件
        table.on('checkbox(currentTable)', function (obj) {
            var data = table.checkStatus('currentTable').data
            if (obj.type == "all") {
                if (obj.checked && table.cache.currentTableId.length != 0) {
                    $('[name="NF-delete2"]').removeClass("layui-hide");
                }
                else {
                    $('[name="NF-delete2"]').addClass("layui-hide");
                }
            }
            else {
                if (data.length > 0) {
                    $('[name="NF-delete2"]').removeClass("layui-hide");
                }
                else {
                    $('[name="NF-delete2"]').addClass("layui-hide");
                }
            }
        });
        //toolbar监听事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            var cachedata = table.cache.currentTableId;
            var data = table.checkStatus('currentTableId').data;
            var parentform = window.name;
            if (obj.event === 'addeqp') {  // 监听删除操作
                if (cachedata.length > 0) {
                    common.modalMsg("主设备已存在", "warning");
                    return false;
                }
                common.modalOpen({
                    title: "选择主设备",
                    url: "/ProcessManage/WorkOrderRun/AddForm?parentform=" + parentform + "&keyValue=" + processId,
                    width: "650px",
                    height: "600px",
                });
            }
            else if (obj.event === 'addother') {
                var ids = [];
                if (cachedata.length == 0) {
                    common.modalMsg("请先添加主设备", "warning");
                    return false;
                }
                if (!cachedata[0].F_IsOtherEqp) {
                    common.modalMsg("主设备不支持加载设备", "warning");
                    return false;
                }
                if (cachedata.length > 1) {
                    for (var i = 1; i < cachedata.length; i++) {
                        ids.push(cachedata[i].F_Id);
                    }
                }
                common.modalOpen({
                    title: "选择加载设备",
                    url: "/ProcessManage/WorkOrderRun/AddForm?parentform=" + parentform + "&keyValue=" + processId + "&materialId=" + materialId + "&ids=" + ids.join(","),
                    width: "650px",
                    height: "600px",
                });
            }
            else if (obj.event === 'delete') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                if (cachedata[0].F_Id == data[0].F_Id && cachedata.length != data.length) {
                    common.modalMsg("主设备无法删除", "warning");
                    return false;
                }
                common.modalConfirm("注：您确定要删除该项数据吗？", function (r) {
                    if (r) {
                        var index = parent.layer.load(0, {
                            shade: [0.5, '#000'], //0.1透明度的背景
                        });
                        try {
                            var cachedata = table.cache.currentTableId;
                            for (var j = 0; j < data.length; j++) {
                                for (var i = 0; i < cachedata.length; i++) {
                                    if (data[j].F_Id == cachedata[i].F_Id) {
                                        if (data[j].F_EqpUse == 7) {
                                            $('#F_MouldNum').parent().parent().addClass('layui-hide');
                                        }
                                        cachedata.splice(i, 1);
                                        break;
                                    }
                                }
                            }
                            table.reload('currentTableId', {
                                data: cachedata
                            });
                            if (cachedata.length == 0)
                            {
                                table.reload('currentTable', {
                                    data: []
                                });
                            }
                            entity = null;
                            common.modalMsg("操作成功", "success");
                        } catch (e) {
                            alert(e);
                        }
                        parent.layer.close(index);
                    }
                    else {
                        return false;
                    }
                });
            }
            return false;
        });
        //toolbar监听事件
        table.on('toolbar(currentTable)', function (obj) {
            var data = table.checkStatus('currentTable').data;
            if (obj.event === 'add2') {  // 监听添加操作
                var cachedata = table.cache.currentTableId;
                if (cachedata.length == 0) {
                    common.modalMsg("请先添加设备", "warning");
                    return false;
                }
                var eqpId = cachedata[0].F_Id;
                var ids = [];
                for (var i = 0; i < data.length; i++) {
                    ids.push(data[i].F_Id);
                }
                common.modalOpen({
                    title: "添加值班人员",
                    url: "/ProcessManage/WorkOrderRun/AddUserForm?ids=" + ids + "&startTime=" + $('#F_PlanStartTime').val() + "&endTime=" + $('#F_PlanEndTime').val() + "&eqpId=" + eqpId,
                    width: "450px",
                    height: "400px",
                });
            }
            else if (obj.event === 'delete2') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                common.modalConfirm("注：您确定要删除该项数据吗？", function (r) {
                    if (r) {
                        var index = parent.layer.load(0, {
                            shade: [0.5, '#000'], //0.1透明度的背景
                        });
                        try {
                            var cachedata = table.cache.currentTable;
                            for (var j = 0; j < data.length; j++) {
                                for (var i = 0; i < cachedata.length; i++) {
                                    if (data[j].F_UserId == cachedata[i].F_UserId) {
                                        cachedata.splice(i, 1);
                                        break;
                                    }
                                }
                            }
                            table.reload('currentTable', {
                                data: cachedata
                            });
                            entity = null;
                            common.modalMsg("操作成功", "success");
                        } catch (e) {
                            alert(e);
                        }
                        parent.layer.close(index);
                    }
                    else {
                        return false;
                    }
                });
            }
            return false;
        });
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var cachedata = table.cache.currentTableId;
            if (cachedata.length == 0) {
                common.modalMsg("未添加设备", "warning");
                return false;
            }
            for (var i = 0; i < cachedata.length; i++) {
                if (cachedata[i].F_EqpUse == 7) {
                    if (data.field.F_MouldNum > cachedata[i].F_MoldingNum) {
                        common.modalMsg("使用数不能大于模块数", "warning");
                        return false;
                    }
                }
            }
            var cachedata2 = table.cache.currentTable;
            if (cachedata2.length == 0) {
                common.modalMsg("未添加值班人员", "warning");
                return false;
            }
            var postData = {};
            postData.keyValue = keyValue;
            postData.F_MouldNum = data.field.F_MouldNum;
            if (!postData.F_MouldNum) {
                delete postData.F_MouldNum;
            }
            postData.listData = JSON.stringify(cachedata);
            postData.userTimeList = JSON.stringify(cachedata2);
            common.submitForm({
                url: '/ProcessManage/WorkOrderRun/SubmitForm?keyValue=' + keyValue,
                param: postData,
                success: function () {
                    common.reloadIframe('/ProcessManage/WorkOrderRun/Index', 'data-search-btn');
                }
            })
            return false;
        });
    });
</script>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">
            <div class="layui-form layuimini-form" lay-filter="adminform">
                <div class="layui-form-item">
                    <div class="layui-col-md4 layui-col-sm4 layui-hide">
                        <label class="layui-form-label">工单编号</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_WorkOrderCode" name="F_WorkOrderCode" maxlength="50" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md4 layui-col-sm4 layui-hide">
                        <label class="layui-form-label">工序名称</label>
                        <div class="layui-input-block">
                            <input id="F_ProcessName" name="F_ProcessName" type="text" class="layui-input" />
                        </div>
                    </div>
                    <div class="layui-col-md4 layui-col-sm4 layui-hide">
                        <label class="layui-form-label">料号编号</label>
                        <div class="layui-input-block">
                            <input id="F_MaterialCode" name="F_MaterialCode" type="text" class="layui-input" />
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-col-md4 layui-col-sm4 layui-hide">
                        <label class="layui-form-label">料号规格</label>
                        <div class="layui-input-block">
                            <input id="F_MaterialSize" name="F_MaterialSize" type="text" class="layui-input" />
                        </div>
                    </div>
                    <div class="layui-col-md4 layui-col-sm4 layui-hide">
                        <label class="layui-form-label">料号名称</label>
                        <div class="layui-input-block">
                            <input id="F_MaterialName" name="F_MaterialName" type="text" class="layui-input" />
                        </div>
                    </div>
                    <div class="layui-col-md4 layui-col-sm4 layui-hide">
                        <label class="layui-form-label">料号单位</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_MaterialUnit" name="F_MaterialUnit" maxlength="50" class="layui-input">
                        </div>
                    </div>

                </div>
                <div class="layui-form-item">
                    <div class="layui-col-md4 layui-col-sm4 layui-hide">
                        <label class="layui-form-label">计划开始时间</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_PlanStartTime" name="F_PlanStartTime" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md4 layui-col-sm4 layui-hide">
                        <label class="layui-form-label">计划结束时间</label>
                        <div class="layui-input-block">
                            <input type="text" id="F_PlanEndTime" name="F_PlanEndTime" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md4 layui-col-sm4 layui-hide">
                        <label class="layui-form-label">数量</label>
                        <div class="layui-input-block">
                            <input type="number" pattern="[0-9]*" id="F_PlanNum" name="F_PlanNum" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-col-md12">
                        <label class="layui-form-label required">作业列表</label>
                        <div class="layui-input-block">
                            <div class="layui-tab">
                                <ul class="layui-tab-title">
                                    <li class="layui-this">设备列表</li>
                                    <li>值班列表</li>
                                </ul>
                                <div class="layui-tab-content">
                                    <div class="layui-tab-item layui-show">
                                        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
                                    </div>
                                    <div class="layui-tab-item">
                                        <table class="layui-hide" id="currentTable" lay-filter="currentTable"></table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item text-right">
                        <div class="layui-inline layui-hide">
                            <label class="layui-form-label required">使用模数</label>
                            <div class="layui-input-inline">
                                <input type="number" pattern="[0-9]*" id="F_MouldNum" name="F_MouldNum" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline text-right">
                            <button class="layui-btn site-demo-active" lay-submit id="submit" lay-filter="saveBtn">确认作业</button>
                        </div>
                    </div>
                </div>
                <script type="text/html" id="toolDemo">
                    <div class="layui-btn-container" id="toolDemo">
                        <button id="NF-add2" name="NF-add2" class="layui-btn layui-btn-sm data-add-btn" lay-event="add2"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button id="NF-delete2" name="NF-delete2" class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn layui-hide" lay-event="delete2"> <i class="layui-icon">&#xe640;</i>删除</button>
                    </div>
                </script>
                <script type="text/html" id="toolbarDemo">
                    <div class="layui-btn-container" id="toolbar">
                        <button id="NF-addeqp" name="NF-addeqp" class="layui-btn layui-btn-sm data-add-btn" lay-event="addeqp"><i class="layui-icon">&#xe654;</i>主设备</button>
                        <button id="NF-addother" name="NF-addother" class="layui-btn layui-btn-sm data-add-btn" lay-event="addother"><i class="layui-icon">&#xe654;</i>加载设备</button>
                        <button id="NF-delete" name="NF-delete" class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn layui-hide" lay-event="delete"> <i class="layui-icon">&#xe640;</i>删除</button>
                    </div>
                </script>
            </div>
    </div>
</body>

